package com.smh.client;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) throws IOException {

        Socket socket = new Socket("127.0.0.1", 6666);
        System.out.println("服务器已经连接成功");
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("==============欢迎来到黑马聊天室================");
            System.out.println("1登录");
            System.out.println("2注册");
            System.out.println("请输入您的选择：");
            String s = scanner.nextLine();
            switch (s) {
                case "1": {
                    login(scanner, socket);
                    break;
                }
                case "2": {
                    register(scanner, socket);
                    break;
                }
                default: {
                    System.out.println("未知选项,请重新输入");
                    break;
                }
            }
        }


    }

    private static void register(Scanner scanner, Socket socket) throws IOException {
        System.out.println("请输入用户名");
        String username = scanner.nextLine();
        System.out.println("请输入密码");
        String password = scanner.nextLine();
        if (!checkUsernameAndPwd(username, password)) {
            return;
        }
        //提交用户名,密码
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        bw.write("2");
        bw.newLine();
        bw.write("username=" + username + "&password=" + password + "\n");
        bw.flush();
        //socket.shutdownOutput();//结束标志
        //判断服务器返回的数据
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String msg = br.readLine();
        System.out.println(msg);
        if (!msg.equals("2")) {
            return;
        }

        //注册成功
        System.out.println("注册成功");

        //监听服务器传回的消息,并打印
        ClientThread clientThread = new ClientThread(socket);
        clientThread.start();
        //聊天实现
        while (true) {
            System.out.println("请输入您要说的话");
            String message = scanner.nextLine();
            bw.write(username + ":" + message + "\n");
            bw.flush();
            //socket.shutdownOutput();//发言结束
        }
    }

    private static void login(Scanner scanner, Socket socket) throws IOException {
        System.out.println("请输入用户名");
        String username = scanner.nextLine();
        System.out.println("请输入密码");
        String password = scanner.nextLine();
        //提交到服务器
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        bw.write("1");//操作标记
        bw.newLine();
        bw.write("username=" + username + "&password=" + password + "\n");
        bw.flush();
        //socket.shutdownOutput();//添加结束标记

        //接受验证结果
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String msg = br.readLine();
        System.out.println(msg);
        if (!msg.equals("1")) {
            return;
        }
        System.out.println("登录成功,开始聊天");

        //监听服务器传回的消息,并打印
        ClientThread clientThread = new ClientThread(socket);
        clientThread.start();
        //聊天实现
        while (true) {
            System.out.println("请输入您要说的话");
            String message = scanner.nextLine();
            bw.write(username + ":" + message + "\n");
            bw.flush();
            //socket.shutdownOutput();//发言结束
        }
    }

    //判断用户名密码是否合法
    private static boolean checkUsernameAndPwd(String username, String password) {
        String regexUsername = "[a-z]{6,18}";//长度：6~18位，纯字母，不能有数字或其他符号。
        String regexPassword = "[a-zA-Z][\\d]{2,7}";//密码长度3~8位。第一位必须是小写或者大小的字母，后面必须是纯数字。
        if (!username.matches(regexUsername)) {
            System.out.println("用户名应为:长度6~18位，纯字母，不能有数字或其他符号。");
            return false;
        }
        if (!password.matches(regexPassword)) {
            System.out.println("密码应为:长度3~8位。第一位必须是小写或者大小的字母，后面必须是纯数字。");
            return false;
        }
        return true;
    }
}
